package deliciouse.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import deliciouse.biz.FoodBiz;
import deliciouse.biz.impl.FoodBizImpl;
import deliciouse.entity.Food;

@WebServlet("/foodlistmanegeupdate")
public class FoodListManegeUpdateServlet extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		int cid = 0;
		int id = 0;
		String  englishname = "";
		String chinesename = "";
		String imagefile = "";
		String description = "";
	
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding("UTF-8");
		try {
			List<FileItem> items = upload.parseRequest(request);
			for (FileItem item : items) {
				if (item.isFormField()) {
					if ("englishname".equals(item.getFieldName())) {
						englishname = item.getString("UTF-8");
					}else if("chinesename".equals(item.getFieldName())){
						chinesename = item.getString("UTF-8");
					}
					else if("id".equals(item.getFieldName())){
						id = Integer.valueOf(item.getString("UTF-8"));
					}
					else if("cid".equals(item.getFieldName())){
						cid = Integer.valueOf(item.getString("UTF-8"));
					}
					else if("description".equals(item.getFieldName())){
						description = item.getString("UTF-8");
					}
					
				} else {
					String clientFilename = item.getName();
					imagefile = System.currentTimeMillis() + clientFilename.substring(clientFilename.lastIndexOf("."));
					String filepath = request.getServletContext().getRealPath("/images/" + imagefile);
					item.write(new File(filepath));
				}
			}
			FoodBiz foodbiz = new FoodBizImpl();
			List<Food> list = foodbiz.getAll();
			Food food = null;
			
			if (imagefile != null) {
				int updatetype = 0;
				for(Food f :list){
					if (id ==f.getId()) {
						updatetype = 2;
						break;
					} else {
						updatetype = 1;
					}
					System.out.println(updatetype);
					switch (updatetype) {
					case 1:
						System.out.print(id);
						food=new Food(0,chinesename,englishname,cid,description,imagefile);
						foodbiz.addfoodlist(food);
						break;
					case 2:
						System.out.print(id);
						food=new Food(chinesename,englishname,cid,description,imagefile,id);
						foodbiz.updatefoodlist(food);
						break;
					}
				}
		
			}
		} catch (Exception e) {
			throw new RuntimeException("上传文件时发生错误！", e);
		}
			response.sendRedirect("foodlistmanege");
	}

}
